package com.java.kids.controller;

import com.java.kids.Service.AppointmentService;
import com.java.kids.Service.CaseService;
import com.java.kids.Service.PatientService;
import com.java.kids.dao.PatientMapper;
import com.java.kids.model.Appointment;
import com.java.kids.model.Case;
import com.java.kids.model.Patient;
import com.java.kids.model.Pc;
import config.AppConfig;
import lib.MessageSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;
import utils.ConfigLoader;

import javax.annotation.Resource;
import java.util.List;

//预约打预防针
@RestController
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private CaseService caseService;
    @Resource
    private PatientMapper patientMapper;

    //预约普通看诊
    @PostMapping("/appointment/doctor")
    public int makeAppointmentToDoctor(@RequestParam("cNo")String cNo,@RequestParam("aTime") String aTime){
        Case aCase=caseService.getMyCases(cNo);
        if (aCase!=null){
            appointmentService.appointment(cNo,aTime);
            return 1;
        }
        return -1;//没有病例，先去建档
    }

    @Autowired
    JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    String from;
    String to = "";
    String subject = "佩珍疫苗预约";
    String text = "";
    //预约打疫苗
    @PostMapping("/appointment/vaccine/{cNo}/{aTime}/{vNo}/{pName}")
    public int makeAppointmentToVaccine(@PathVariable("cNo")String cNo,
                                        @PathVariable("aTime") String aTime,
                                        @PathVariable("vNo") int vNo,
                                        @PathVariable("pName") String pName){
        Case aCase=caseService.getMyCases(cNo);
        Patient patient = patientMapper.getPatientByUsername(pName);
        List<Appointment> allAppointment = appointmentService.findPatientAllAppointment(pName);
        System.out.println(cNo+aTime+vNo+pName);
        System.out.println(aCase);
        if (aCase==null){
            to=patient.getPphone();
            text = allAppointment.get(0).getvNo();
            int i = appointmentService.appointmentVaccine(cNo, aTime, vNo, pName);
            if(i == 1){
            //短信提示
            /*  AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
                MessageSend submail = new MessageSend(config);
                submail.addTo(patient.getPphone());
                submail.addContent("【星哥管理系统】您的业务已成功受理");
                String send = submail.send();
                System.out.println("接口返回数据："+send);
            */
            //邮箱提示
                System.out.println("邮箱： "+to);
            try {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom(from);
                message.setTo(to);
                message.setSubject(subject);
                message.setText("你好！你预约的<"+text+">已成功受理！！！");
                mailSender.send(message);
                return 1;
            } catch (MailException e) {
                e.printStackTrace();
                return -1;
            }
            }
            return -1;
        }
        return -1;
    }

    //更改预约时间
    @PutMapping("/modifyTime/{aTime}/{cNo}")
    public int modifyAppointment(@PathVariable String aTime,
                                 @PathVariable String cNo){
        List<Appointment> appointment=appointmentService.findAppointment(cNo);
        if (appointment.size()!=0){
            System.out.println("修改时间: "+aTime);
            appointmentService.modifiedTime(aTime,cNo);
            return 1;
        }
        return -1;
    }

//    病人查找预约的所有预约时间
    @GetMapping("/patientAppointment")
    public List<Appointment> getAllAppointment(@RequestParam("pusername") String pusername){
        List<Appointment> appointments=appointmentService.findPatientAllAppointment(pusername);
        if (appointments!=null){
            return appointments;
        }
        return null;
    }
    //获取所有病人的预约信息
    @GetMapping("/patientAppointments")
    public List<Appointment> getAllAppointments(){
        List<Appointment> appointments=appointmentService.findPatientAllAppointments();
        if (appointments!=null){
            return appointments;
        }
        return null;
    }

    //医生查看所有的被预约时间,大于当前系统的时间，看mybatis的语句 > now()
    /*@GetMapping("/doctorAppointmentTime")
    public List<Appointment> getALLDAppointment(@RequestParam("dusername") String dusername){
        List<Appointment> appointments=appointmentService.findDoctorAllAppointment(dusername);
        return appointments;
    }*/
    //查看当天的预约
    @GetMapping("/doctor/current")
    public List<Appointment> currentAppointment(@RequestParam("dusername") String dusername){
        List<Appointment> appointments=appointmentService.findCurrentAppointment(dusername,"","");
        if (appointments!=null){
            return appointments;
        }
        return null;
    }
    //删除该预约
    @GetMapping("/appointment/del")
    public int delThisAppointment(@RequestParam("cNo") String cNo){
        System.out.println(cNo);
        if (appointmentService.deleteAppointment(cNo)==1){
            return 1;
        }
        return -1;
    }
}
